﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace card
{
    public partial class CycleCLHistoryForm : Form
    {
        public static WebDG.Service ws = new card.WebDG.Service();

        public CycleCLHistoryForm()
        {
            InitializeComponent();
        }

        private void CycleCLLRForm_Load(object sender, EventArgs e)
        {
            LoadStack();
        }

        private void LoadStack()
        {
            DataSet ds = ws.getDataSet_Serv("SELECT ID, STACKNAME FROM TRUCK_CYCLE_STACK WHERE ID IN " +
                "(SELECT DISTINCT STACKID FROM TRUCK_CYCLE_STACK_MX WHERE ISDEL = '1') order by STACKNAME");
            this.dataGridViewStack.AutoGenerateColumns = false;
            this.dataGridViewStack.DataSource = ds.Tables[0];
        }

        private void LoadTruckMX()
        {
            if (this.dataGridViewStack.SelectedRows != null && this.dataGridViewStack.SelectedRows.Count > 0)
            {
                string strStackID = this.dataGridViewStack.SelectedRows[0].Cells["ID"].Value.ToString();
                string strSQL = "select ID, STACKID, TRUCKNO from TRUCK_CYCLE_STACK_MX where STACKID = {0} and ISDEL = '1' order by TRUCKNO";
                DataSet ds = ws.getDataSet_Serv(string.Format(strSQL, strStackID));
                this.dataGridViewTruck.AutoGenerateColumns = false;
                this.dataGridViewTruck.DataSource = ds.Tables[0];
            }
            else
            {
                this.dataGridViewTruck.DataSource = null;
            }
        }

        private void dataGridViewStack_SelectionChanged(object sender, EventArgs e)
        {
            if (this.dataGridViewStack.SelectedRows != null)
            {
                LoadTruckMX();
            }
        }

        private string create_id()
        {
            string sequence = "";
            string sqlStr = "select SEQ_TRUCK.nextval from dual";
            DataSet ds1 = ws.getDataSet_Serv(sqlStr);
            if (ds1.Tables[0].Rows.Count > 0)
            {
                sequence = ds1.Tables[0].Rows[0][0].ToString();
            }
            string year_str = DateTime.Now.Year.ToString();
            string month_str = DateTime.Now.Month.ToString();
            string day_str = DateTime.Now.Day.ToString();
            string id = year_str + month_str + day_str + sequence;
            return id;
        }

        /// <summary>
        /// 恢复垛名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonReStack_Click(object sender, EventArgs e)
        {
            if (this.dataGridViewStack.SelectedRows != null && this.dataGridViewStack.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in this.dataGridViewStack.SelectedRows)
                {
                    string strID = row.Cells["ID"].Value.ToString();
                    ws.execute_UID_Serv("update TRUCK_CYCLE_STACK_MX set ISDEL='0',SFTIME=null,ADDMAN='" + Session.Operator_User + "',ADDTIME=SYSDATE where STACKID=" + strID);
                    ws.execute_UID_Serv("update TRUCK_CYCLE_STACK set ISDEL='0',ADDMAN='" + Session.Operator_User + "',ADDTIME=SYSDATE where ID = " + strID);
                }
                MessageBox.Show("恢复成功！");
                LoadStack();
            }
        }

        /// <summary>
        /// 恢复车号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonReTruck_Click(object sender, EventArgs e)
        {
            if (this.dataGridViewTruck.SelectedRows != null && this.dataGridViewTruck.SelectedRows.Count > 0)
            {
                string strSTACKID = "", _tempID = "";
                foreach (DataGridViewRow row in this.dataGridViewTruck.SelectedRows)
                {
                    strSTACKID = row.Cells["STACKID"].Value.ToString();
                    if (_tempID != strSTACKID)
                    {
                        _tempID = strSTACKID;
                        ws.execute_UID_Serv("update TRUCK_CYCLE_STACK set ISDEL='0' where ID = " + strSTACKID);
                    }
                    string strID = row.Cells["MXID"].Value.ToString();
                    ws.execute_UID_Serv("update TRUCK_CYCLE_STACK_MX set ISDEL='0',SFTIME=null,ADDMAN='" + Session.Operator_User + "',ADDTIME=SYSDATE where ID=" + strID);
                }
                MessageBox.Show("恢复成功！");
                LoadTruckMX();
            }
        }
    }
}
